package cn.xxstudy.blog.controller

import cn.xxstudy.blog.bean.LinkInfo
import cn.xxstudy.blog.entity.Blog
import cn.xxstudy.blog.service.BlogService
import cn.xxstudy.blog.service.CategoryService
import cn.xxstudy.blog.service.TagsService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*

/**
 * 作者：wuxinxi on 2020/11/27
 * 包名：cn.xxstudy.blog.controller
 * TODO:博客首页
 */
@Controller
class IndexController {
    companion object {
        private const val BLOG_INDEX = "index"
        private const val BLOG_SEARCH = "search"
        private const val BLOG_FOOTER_LIST = "fragment_base::newBlogList"
    }

    @Autowired
    private lateinit var blogService: BlogService

    @Autowired
    private lateinit var categoryService: CategoryService

    @Autowired
    private lateinit var tagsService: TagsService

    @GetMapping("/")
    fun index(@PageableDefault(size = 8, sort = ["updateDateTime", "lookCount", "recommendChecked", ], direction = Sort.Direction.DESC) pageable: Pageable, model: Model): String {
        model.addAttribute("blogListPage", blogService.listBlog(pageable))
        model.addAttribute("categoryTop", categoryService.listCategoryTop(6))
        model.addAttribute("tagsTop", tagsService.listTagsTop(5))
        model.addAttribute("blogTop", blogService.listRecommendBlogTop(10))
        link(model)
        return BLOG_INDEX
    }

    @RequestMapping("/search")
    fun search(@PageableDefault(size = 5, direction = Sort.Direction.DESC, sort = ["updateDateTime"], ) pageable: Pageable,
               @RequestParam searchKey: String, model: Model): String {
        model.addAttribute("blogListPage", blogService.listBlog("%$searchKey%", pageable))
        model.addAttribute("searchKey", searchKey)
        return BLOG_SEARCH
    }

    @GetMapping("/footer/blog")
    fun newBlog(model: Model): String {
        model.addAttribute("newBlogList", blogService.listNewBlogTop(3))
        return BLOG_FOOTER_LIST
    }

    fun link(model: Model) {
        val linkList = mutableListOf<LinkInfo>()
        linkList.add(LinkInfo("玩Android", "https://www.wanandroid.com/"))
        linkList.add(LinkInfo("阿里巴巴矢量图库", "https://www.iconfont.cn/"))
        linkList.add(LinkInfo("API接口调试", "http://coolaf.com/"))
        linkList.add(LinkInfo("程序员工具", "https://tool.lu/"))
        linkList.add(LinkInfo("Android源码", "http://androidxref.com/"))
        linkList.add(LinkInfo("UI配色", "https://www.webdesignrankings.com/resources/lolcolors/"))
        linkList.add(LinkInfo("CTOLib码库", "https://ctolib.com/"))
        linkList.add(LinkInfo("uTools", "https://www.u.tools/"))
        model.addAttribute("linkList", linkList)
    }

}